﻿Imports System.IO
Imports System.Xml


Public Class frmParamProtocole

    Dim xmlPath As String

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Try
            OpenFileDialog1.InitialDirectory = FormMain.NSDossier
            OpenFileDialog1.Filter = "Fichiers XML|*.XML"
            OpenFileDialog1.ShowDialog()
            Dim xFilePath = OpenFileDialog1.FileName
            If xFilePath <> "" Then
                Cursor.Current = Cursors.WaitCursor()

                xmlPath = xFilePath
                Dim XmlDoc As XmlDocument = New Xml.XmlDocument()

                Try
                    XmlDoc.Load(xmlPath)
                    Dim root As XmlElement = XmlDoc.DocumentElement
                    Dim oXmlNode As XmlNode
                    Dim i As Short = 0

                    Dim nodeList As XmlNodeList
                    Dim strProtocoles As String
                    strProtocoles = ""
                    nodeList = root.ChildNodes

                    For Each oXmlNode In nodeList
                        strProtocoles = strProtocoles & oXmlNode.Attributes.Item(0).Value & ";"
                        i = i + 1
                    Next

                    chargerCombo(strProtocoles, Me.CmbProt, i)
                    Cursor.Current = Cursors.Default
                    Me.CmbProt.Focus()
                    MsgBox(DirectCast(My.Resources.Images.alertListLoad, String), MsgBoxStyle.Information, "Pocket eRelevé")

                Catch Excep As Exception
                    MsgBox("erreur")
                    Cursor.Current = Cursors.Default
                    MsgBox(DirectCast(My.Resources.Images.errorOnLoadFile, String) + Excep.Message, MsgBoxStyle.Exclamation, "Pocket eRelevé")
                End Try

            Else
                MsgBox(DirectCast(My.Resources.Images.alertFileNotSelected, String), MsgBoxStyle.Information, "Pocket eRelevé")
            End If
        Catch Excep As Exception
            MsgBox("erreur")
            Cursor.Current = Cursors.Default
            MsgBox(DirectCast(My.Resources.Images.errorOnLoadFile, String) + Excep.Message, MsgBoxStyle.Exclamation, "Pocket eRelevé")
        End Try
    End Sub

    Private Sub chargerCombo(ByVal strProtocoles As String, ByVal ctlDataSource As System.Windows.Forms.Control, ByVal a As Short)
        Dim j As Short
        CType(ctlDataSource, System.Windows.Forms.ComboBox).Items.Clear()
        If strProtocoles <> "" Then
            For j = 0 To a - 1
                CType(ctlDataSource, System.Windows.Forms.ComboBox).Items.Add(Split(strProtocoles, ";")(j))
            Next
        End If

    End Sub

    Private Sub BtnNouvProt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        ' frmSaisieProtocole.Show()
        Me.Hide()
    End Sub

    'Private Sub CmbProt_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbProt.SelectedValueChanged
    '    Dim strProtocoleName As String
    '    strProtocoleName = CmbProt.SelectedValue
    '    ChargerProtocole(xmlPath, strProtocoleName)
    'End Sub

    Private Sub ChargerProtocole(ByVal xmlFile As String, ByVal NomProtocole As String)
        Dim XmlDoc As XmlDocument = New Xml.XmlDocument()

        Try

            XmlDoc.Load(xmlFile)
            Dim root As XmlElement = XmlDoc.DocumentElement
            Dim oXmlNode As XmlNode
            Dim i As Short = 0

            Dim nodeList As XmlNodeList
            Dim strProtocoles As String
            strProtocoles = ""
            oXmlNode = root.SelectSingleNode("/PROTOCOLE/" & NomProtocole)
            nodeList = oXmlNode.ChildNodes
            For Each oXmlNode In nodeList
                strProtocoles = strProtocoles & oXmlNode.InnerText & ";"
                i = i + 1
            Next
        Catch Excep As Exception
            MsgBox("erreur")
            Cursor.Current = Cursors.Default
            MsgBox(DirectCast(My.Resources.Images.errorOnLoadFile, String) + Excep.Message, MsgBoxStyle.Exclamation, "Pocket eRelevé")
        End Try

    End Sub

    Private Sub btnAff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmEntryProtocol.Show()
        Me.Hide()

    End Sub

    Private Sub btnMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMenu.Click
        FormMain.Show()
        Me.Hide()
    End Sub

    Private Sub btnReturn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReturn.Click
        frmParamChoixList.Show()
        Me.Hide()
    End Sub

End Class